package com.starblink.android.common.web.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.skydoves.balloon.Balloon
import com.starblink.android.basic.bridge.BridgeCommand
import com.starblink.android.basic.bridge.BridgeUtil
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.android.common.R
import com.starblink.basic.storage.SkStorage
import com.starblink.basic.util.ActivityStackManage

class FunWebDialog(var context: Context,
                   val isProduct:Boolean = true,
                   private var balloon:Balloon,
                   private val clickFollowStore:(view:View)->Unit,
                   private val clickShare:()->Unit,
                   private val clickRefresh:()->Unit,
                   private val findSimilar:()->Unit,
                   private val priceCheck:()->Unit,
                   private val copyLink:()->Unit,){
    val rlViewWishlist: RelativeLayout by lazy { balloon.getContentView().findViewById(R.id.rl_view_wishlist) }
    val rlFollowStore: RelativeLayout by lazy { balloon.getContentView().findViewById(R.id.rl_follow_store) }
    val viewFollowStore: View by lazy { balloon.getContentView().findViewById(R.id.view_follow_store) }
    private val tvFollowTip:TextView by lazy { balloon.getContentView().findViewById(R.id.tv_follow_tip) }
    private val ivFollowTip:ImageView by lazy { balloon.getContentView().findViewById(R.id.iv_follow_tip) }
    val rlShareWeb: RelativeLayout by lazy { balloon.getContentView().findViewById(R.id.rl_share_web) }
    val rlReloadPage: RelativeLayout by lazy { balloon.getContentView().findViewById(R.id.rl_reload_page) }
    val rlCopyLink: RelativeLayout by lazy { balloon.getContentView().findViewById(R.id.rl_copy_link) }
    val rlFindSimilar: RelativeLayout by lazy { balloon.getContentView().findViewById(R.id.rl_find_similar) }
    val vFindSimilar: View by lazy { balloon.getContentView().findViewById(R.id.v_find_similar) }
    val rlPriceCheck: RelativeLayout by lazy { balloon.getContentView().findViewById(R.id.rl_price_check) }
    val vPriceCheck: View by lazy { balloon.getContentView().findViewById(R.id.v_price_check) }
    init {
        if(isProduct){
            vFindSimilar.visibleOrGone(true)
            rlFindSimilar.visibleOrGone(true)
            rlPriceCheck.visibleOrGone(true)
            vPriceCheck.visibleOrGone(true)
        }
        handleVM()
    }

    fun handleVM(){
        rlViewWishlist.setOnClickListener {
            //跳转心愿单
            balloon.dismiss()
            ActivityStackManage.finishAllActivity()
            SkStorage.put(UserDataCenter.HOME_TAB_INDEX_NOSPM, true)
            BridgeUtil.sendCommonData(BridgeCommand.Common.toHomeWish, "") //状态同步给H5
        }
        rlFollowStore.setOnClickListener{
            //Follow店铺
            balloon.dismiss()
            clickFollowStore.invoke(it)
        }
        rlShareWeb.setOnClickListener {
            //分享
            balloon.dismiss()
            clickShare.invoke()
        }
        rlReloadPage.setOnClickListener {
            //刷新页面
            balloon.dismiss()
            clickRefresh.invoke()
        }
        rlFindSimilar.setOnClickListener {
            //找相似
            balloon.dismiss()
            findSimilar.invoke()
        }
        rlPriceCheck.setOnClickListener {
            //比价
            balloon.dismiss()
            priceCheck.invoke()
        }
        rlCopyLink.setOnClickListener {
            //复制链接
            balloon.dismiss()
            copyLink.invoke()
        }
    }

    fun followStore(subOrNot: Boolean){
        tvFollowTip.text=if(subOrNot) "Unfollow the store" else "Follow the store"
        ivFollowTip.setImageResource(if (subOrNot) R.drawable.ic_svg_web_followed else R.drawable.ic_svg_web_follow)
    }

}